/*AyflEomXWyqCHfdTxzWda+bKRNxqoRhrf02Huevr4hQ=*/
//主子表列表

import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { createPage, ajax, base, high, print, toast, cacheTools, cardCache, getMultiLang,createPageIcon } from 'nc-lightapp-front';
import { buttonClick, initTemplate, searchBtnClick, pageInfoClick, tableModelConfirm, doubleClick } from './events';
import { tableId, searchId, nodekey, billType, pkname, dataSource, searchKey } from './constants';
import { buttonVisible, getButtonsKey, onListButtonControl } from '../../../public/components/pubUtils/buttonvisible';
import linkSourceList from '../../../public/components/linkSourceList.js';
import afterEvent from '../../../public/components/searchAreaAfterEvent'; //查询区编辑后事件
import {listSocketConnect} from '../../../public/components/pubUtils/MicroServiceSocket.js';
const { BillTrack, PrintOutput} = high;
import NCUploader from 'uap/common/components/NCUploader';
let { setDefData, getDefData } = cardCache;
const { NCDiv } = base;
class List extends Component {
	constructor(props) {
		super(props);
		this.state = {
			showBillTrack: false, //单据追溯模态框的状态标志
			showUploader: false, //附件管理
			target: null, //附件管理
			json: {}
		};
		this.printData = {
			billtype: billType, //单据类型
			appcode: props.getSearchParam('c'), //功能节点编码，即模板编码
			nodekey: nodekey, //模板节点标识
			oids: null, // 功能节点的数据主键
			userjson: billType //单据类型,billtype不是必需字段，后台没有设置接收字段，以userjson代替
		};
		this.outputData = {
			billtype: billType, //单据类型
			appcode: props.getSearchParam('c'), //功能节点编码，即模板编码
			funcode: props.getSearchParam('c'),
			nodekey: nodekey, //模板节点标识
			oids: null, // 功能节点的数据主键
			userjson: billType, //单据类型,billtype不是必需字段，后台没有设置接收字段，以userjson代
			outputType: 'output'
		};
		this.searchId = searchId;
		this.tableId = tableId;
		this.billType = billType;
		this.pkname = pkname;
		this.pageId = props.getSearchParam('p');
		let { form, button, table, insertTable, search } = this.props;
		let { setSearchValue, setSearchValByField, getAllSearchData } = search;
		this.setSearchValByField = setSearchValByField; //设置查询区某个字段值
		this.getAllSearchData = getAllSearchData; //获取查询区所有字段数据
		this.Info = {
			allButtonsKey: [],
			pk_bill: null, //当前选中第一行的主键pk
			pk_tradetype: null, //当前选中第一行行的交易类型code
			billno: null //当前选中第一行的单据号
		};
		this.dataInSaga = {    //用于saga里面的busiinfo
			appcode: props.getSearchParam('c') ? props.getSearchParam('c') : null,
			pagecode: props.getSearchParam('p') ? props.getSearchParam('p') : null,
			butncode: null
		}
		props.use.table(tableId);
		props.use.search(searchId);
	}

	componentWillMount() {
		let callback = (json) => {
			this.setState({ json: json }, () => {
				initTemplate.call(this, this.props, this.initShow);
			});
		};
		getMultiLang({ moduleId: [ 'payablebill', 'public' ], domainName: 'arap', currentLocale: 'simpchn', callback });
	}

	//在第一次渲染后调用
	componentDidMount() {}

	getPagecode = () => {
		let pagecode = '200805EPB_LIST';
		return pagecode;
	};

	initShow = () => {
		//联查

		if (!this.props.table.hasCacheData(dataSource)) {
			this.onSelected(); //缓存不存在，就控制按钮
			if ('browse' == this.props.getUrlParam('status')) {
				if ('fip' == this.props.getUrlParam('scene')) {
					linkSourceList(this.props, this.tableId);
				}
			}
		}
	};

	componentWillReceiveProps(nextProps) {}

	//列表控制按钮
	onSelected = () => {
		onListButtonControl(this);
	};

	//打印
	onPrint = () => {
		print(
			'pdf', //支持两类: 'html'为模板打印, 'pdf'为pdf打印
			'/nccloud/arap/arappub/print.do', //后台服务url
			this.printData,
			false
		);
	};

	//正式打印
	officalPrintOutput = () => {
		ajax({
			url: '/nccloud/arap/arappub/officialPrint.do',
			data: this.printData,
			async: false,
			success: (res) => {
				if (res.success) {
					print(
						'pdf', //支持两类: 'html'为模板打印, 'pdf'为pdf打印
						'/nccloud/arap/arappub/print.do', //后台服务url
						this.printData,
						false
					);
				}
			}
		});
	};
	//取消正式打印
	cancelPrintOutput = () => {
		ajax({
			url: '/nccloud/arap/arappub/cancelPrint.do',
			data: this.printData,
			success: (res) => {
				if (res.success) {
					//取消正式打印提示框
					toast({ color: 'success', content: res.data });
				}
			}
		});
	};

	//打印输出
	printOutput = () => {
		this.refs.printOutput.open();
	};
	//输出成功回调函数
	onSubmit() {}
	// 附件的关闭点击
	onHideUploader = () => {
		this.setState({
			showUploader: false
		});
	};

	// 查询区渲染完成回调函数
	renderCompleteEvent = () => {
		let cachesearch = getDefData(searchKey, dataSource);
		if (cachesearch && cachesearch.conditions) {
			// this.props.search.setSearchValue(this.searchId, cachesearch);
			for (let item of cachesearch.conditions) {
				if (item.field == 'billdate') {
					// 时间类型特殊处理
					let time = [];
					time.push(item.value.firstvalue);
					time.push(item.value.secondvalue);
					this.props.search.setSearchValByField(this.searchId, item.field, {
						display: item.display,
						value: time
					});
				} else {
					this.props.search.setSearchValByField(this.searchId, item.field, {
						display: item.display,
						value: item.value.firstvalue
					});
				}
			}
		}
	};

	render() {
		let { table, button, search } = this.props;
		let buttons = this.props.button.getButtons();
		let multiLang = this.props.MutiInit.getIntl(this.moduleId);
		let { createSimpleTable } = table;
		let { NCCreateSearch } = search;
		let { createButton, getButtons } = button;
		let { showUploader, target } = this.state;
		const { createBillHeadInfo } = this.props.BillHeadInfo;
		return (
			<div className="nc-bill-list">
				{listSocketConnect.call(this)}
				<NCDiv areaCode={NCDiv.config.HEADER} className="nc-bill-header-area">
					<div className="header-title-search-area">
						{createBillHeadInfo({
							title:this.state.json['payablebill-000032'],//国际化处理： 暂估应付单管理
							initShowBackBtn: false
						})}
					</div>
					<div className="header-button-area">
						{this.props.button.createButtonApp({
							area: 'list_head',
							buttonLimit: 3,
							onButtonClick: buttonClick.bind(this),
							popContainer: document.querySelector('.header-button-area')
						})}
					</div>
				</NCDiv>
				<div className="nc-bill-search-area">
					{NCCreateSearch(
						this.searchId, //模块id
						{
							clickSearchBtn: searchBtnClick.bind(this), //   点击按钮事件
							showAdvBtn: true, //  显示高级按钮
							onAfterEvent: afterEvent.bind(this), //编辑后事件
							renderCompleteEvent: this.renderCompleteEvent // 查询区渲染完成回调函数
						}
					)}
				</div>
				<div className="nc-bill-table-area">
					{createSimpleTable(this.tableId, {
						fieldid: "estipayablebill",
						dataSource: dataSource,
						pkname: pkname,
						handlePageInfoChange: pageInfoClick.bind(this),
						tableModelConfirm: tableModelConfirm,
						onRowDoubleClick: doubleClick.bind(this),
						showCheck: true,
						showIndex: true,
						onSelected: this.onSelected.bind(this),
						onSelectedAll: this.onSelected.bind(this),
						crossPageSelect:true,
						selectedChange:(...params)=>{
							this.onSelected.call(this)
						   },
						high_selectable: {
							enable: true  //默认为true
						},
						// 框选
						onBatchSelected: (...params) => {
							this.onSelected.call(this)
						},
						componentInitFinished: () => {
							this.onSelected();
						}
					})}
				</div>
				{/* 单据追溯组件 */}
				<BillTrack
					show={this.state.showBillTrack}
					close={() => {
						this.setState({ showBillTrack: false });
					}}
					pk={this.Info.pk_bill} //单据id
					type={this.Info.pk_tradetype} //单据类型
				/>
				{showUploader && (
					<NCUploader
						billId={this.Info.pk_bill}
						billNo={this.Info.billno}
						childAppcode={this.props.getSearchParam('c')} 
						childPagecode={this.getPagecode()}
						target={target}
						placement={'bottom'}
						onHide={this.onHideUploader}
					/>
				)}
				{/* {打印输出} */}
				<PrintOutput
					ref="printOutput"
					url="/nccloud/arap/arappub/outPut.do"
					data={this.outputData}
					callback={this.onSubmit}
				/>
			</div>
		);
	}
}

List = createPage({
	mutiLangCode: '2052'
})(List);

export default List;

/*AyflEomXWyqCHfdTxzWda+bKRNxqoRhrf02Huevr4hQ=*/